package com.djl.mymall.mbg.controller;


import com.djl.mymall.common.CommonResult;
import com.djl.mymall.dto.AdminUserDetails;
import com.djl.mymall.entity.UserLoginParamers;
import com.djl.mymall.mbg.entity.Admin;
import com.djl.mymall.mbg.entity.AdminLoginLog;
import com.djl.mymall.mbg.entity.Permission;
import com.djl.mymall.mbg.service.IAdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 后台用户表 前端控制器
 * </p>
 *
 * @author djl
 * @since 2022-01-06
 */
@Controller
@Api(tags = "AdminController",description = "后台用户管理")
@RequestMapping("/admin")
public class AdminController {

    @Value("${jwt.tokenHeader}")
    private String tokenHeader;
    @Autowired
    private IAdminService adminService;
    @ApiModelProperty("注册")
    @ResponseBody
    @PostMapping("/register")
    public CommonResult register(@RequestBody Admin admin){
        Admin adminRegister = adminService.register(admin);
        if (null==admin){
            return  CommonResult.failed(adminRegister);
        }
        return  CommonResult.success(adminRegister);
    }


    @ApiOperation(value = "登录后返回token")
    @PostMapping("/login")
    @ResponseBody
    public   CommonResult login(@RequestBody UserLoginParamers adminLoginParam){

        String token = adminService.login(adminLoginParam.getUsername(), adminLoginParam.getPassword());
        if (null==token){
            return CommonResult.validateFailed("用户名或密码错误");
        }
        Map<String,String> tokenMap=new HashMap<>();
        tokenMap.put("token",token);
        tokenMap.put("tokenHead",tokenHeader);
        return CommonResult.success(tokenMap);
    }


    @ApiOperation("获取用户所有权限（包括+-权限）")
    @RequestMapping(value = "/permission/{adminId}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<Permission>> getPermissionList(@PathVariable("adminId") long adminId){
        List<Permission> permissionList = adminService.getPermissionList(adminId);
        if (null==permissionList){
            return CommonResult.validateFailed("获取全限失败");
        }
        return CommonResult.success(permissionList);
    }






}
